import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-07-01
 * Time：14:45
 */
public class RedisDemoHash {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()) {
            test5(jedis);
        }
    }

    private static void test5(Jedis jedis) {
        System.out.println("hmser/hmget");
        jedis.flushDB();

        Map<String, String> hash = new HashMap<>();
        hash.put("f1", "111");
        hash.put("f2", "222");
        hash.put("f3", "333");
        jedis.hmset("key", hash);

        List<String> result = jedis.hmget("key", "f1", "f2", "f3");
        System.out.println(result);
    }

    private static void test4(Jedis jedis) {
        System.out.println("hkeys/hvals");
        jedis.flushDB();

        Map<String, String> hash = new HashMap<>();
        hash.put("f1", "111");
        hash.put("f2", "222");
        hash.put("f3", "333");
        jedis.hmset("key", hash);

        Set<String> keys = jedis.hkeys("key");
        System.out.println(keys);

        List<String> vals = jedis.hvals("key");
        System.out.println(vals);

    }

    private static void test3(Jedis jedis) {
        System.out.println("hdel");
        jedis.flushDB();

        Map<String, String> hash = new HashMap<>();
        hash.put("f1", "111");
        hash.put("f2", "222");
        hash.put("f3", "333");
        jedis.hmset("key", hash);

        System.out.println(jedis.hgetAll("key"));

        System.out.println("del: " + jedis.hdel("key", "f1", "f2"));
        System.out.println(jedis.hgetAll("key"));
    }

    private static void test2(Jedis jedis) {
        System.out.println("hexists");
        jedis.flushDB();

        jedis.hset("key", "f1", "111");
        Map<String, String> hash = new HashMap<>();
        hash.put("f2", "222");
        hash.put("f3", "333");
        jedis.hmset("key", hash);

        System.out.println(jedis.hexists("key", "f1"));
        System.out.println(jedis.hexists("key", "f100"));
    }

    private static void test1(Jedis jedis) {
        System.out.println("hset/hmset/hget");
        jedis.flushDB();

        jedis.hset("key", "f1", "111");
        Map<String, String> hash = new HashMap<>();
        hash.put("f2", "222");
        hash.put("f3", "333");
        jedis.hmset("key", hash);

        String result = jedis.hget("key", "f1");
        System.out.println(result);

        result = jedis.hget("key", "f2");
        System.out.println(result);

        result = jedis.hget("key", "f3");
        System.out.println(result);
    }
}
